from user import *
#NOTICE: This is the top level module, do not import this file

def Play(request,sname):
    data = ServiceCore.objects.get(name = sname)
    if (data.activate == True):
        gnode = etree.parse(CONFIG.SERVICES_PATH + sname+'/config.xml')
        gallery = Gallery.InitGalleryConfig(gnode.getroot())
        layout = Layout.InitLayoutConfig(gnode.getroot())
        template = layout.GetTemplate()
        gname = gallery.GetDefault()

        if (str(template) == 'default'):
          template = 'contrib'
        play_t = loader.get_template('comp/' + template + '/photoplayer.html')

        if(gname):
          c = Context({'GALLERY':gname,'SERVICE':sname,'PHOTOS':gallery.BasicInfo()[gname]})
          return HttpResponse(play_t.render(c),mimetype="text/html")
        else:
          return HttpResponse("Default Gallery Not Specified")
    else:
        return HttpResponse("Service Not Activated") 


def Default(request,sname):
    aut = HasAuthority(request,sname)
    if (aut['r'] == False):
        return HttpResponse(aut['m'])
    gname = "" 
    if (request.REQUEST.has_key("gname")):
      gname = request.REQUEST['gname']
    else:
      return HttpResponse("Gallery Not Found")
    data = ServiceCore.objects.get(name = sname)
    if (data.activate == True):
        gnode = etree.parse(CONFIG.SERVICES_PATH + sname+'/config.xml')
        gallery = Gallery.InitGalleryConfig(gnode.getroot())
        if gallery.SetDefault(gname):
          SaveConfig(sname,gnode)
          return GeneralXMLResponse(request,{},
            "Default Gallery For Play Is Set Successfully")
        else:
          return HttpResponse("Gallery %s Not Found"%(gname))
    else:
        return HttpResponse("Service Not Activated") 


